package com.oasistudio.tk.connector.imp;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oasistudio.tk.connector.TopAPIConnectorHelper;
import com.oasistudio.tk.connector.TopApiRequest;
import com.oasistudio.tk.connector.TopApiResponse;
import com.taobao.tbw.common.TBWContext;

public class CollectShopConnector implements TopAPIConnectorHelper {

	public CollectShopConnector() {
	}

	@Override
	public Object asyncPaser(byte[] part) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getApiUrl() {
		TopApiRequest request = new TopApiRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		TreeMap<String, String> params = new TreeMap<String, String>();
		//系统参数
		params.put("method", "taobao.favorite.search");
		params.put("session", TBWContext.getInstance().getTopsession());
		params.put("timestamp", sdf.format(calendar.getTime()));
		params.put("format", "json");
		params.put("app_key", TBWContext.getInstance().getAppKey());
		params.put("v", "2.0");
		params.put("partner_id", "top-apitools");
		params.put("sign_method", "md5");
		//API method 参数
		params.put("user_nick", TBWContext.getInstance().getNick());
		params.put("collect_type", "SHOP");//ITEM或SHOP
		params.put("page_no", "1");
		request.params = params;
		return request.generalRequestUrl(true);
	}
	
	@Override
	public Object syncPaser(byte[] all) {
		CollectShops collectInfo = new CollectShops();
		try {
			TopApiResponse response = new TopApiResponse("favorite_search_response");
			String str = new String(all, "UTF-8");
			//Log.d("return msg:", str);
			if (response.parseResult(str).success) {
				collectInfo.isSuccess = true;
				JSONObject jsObj = response.data;
				collectInfo.totalResults = jsObj.optInt("total_results");
				if (jsObj.has("has_next")) {
					collectInfo.hashNext = jsObj.optBoolean("has_next");
				}
				if (jsObj.has("collect_items")) {
					JSONObject tradesJSON = jsObj.getJSONObject("collect_items");
					if (tradesJSON.has("collect_item")) {
						JSONArray tradesArray = tradesJSON.getJSONArray("collect_item");
						Shop[] shops = new Shop[tradesArray.length()];
						collectInfo.shops = shops;
						for (int i = 0; i < tradesArray.length(); i++) {
							JSONObject trade = tradesArray.getJSONObject(i);
							shops[i] = new Shop();
							shops[i].shopId = trade.optLong("item_numid");
							shops[i].owner_nick = trade.optString("item_owner_nick");
							shops[i].title = trade.optString("title");
						}
					}
				}
			} else {
				collectInfo.isSuccess = false;
				collectInfo.errCode = response.errCode;
				collectInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return collectInfo;
	}

	public static class CollectShops {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public Number totalResults;
		public Shop[] shops;//?��??????		
		public Boolean hashNext;
	}

	public static class Shop {
		public Number shopId;// �???��?id
		public String owner_nick;//�?��Nick
		public String title;//�????��
		//public String commission;//�??佣�?�??�?��??????�?���??�??
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("shopId:"+shopId);
			sb.append(";owner_nick:"+owner_nick);
			sb.append(";title:"+title);
			return sb.toString();
		}
	}
}
